package com.bwie.controller;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.bwie.domain.TbUser;
import com.bwie.mapper.TbUserMapper;
import com.bwie.service.TbUserService;
import com.bwie.utils.R;
import com.bwie.vo.LoginInfoVo;
import com.bwie.vo.PageInfoVo;
import lombok.extern.slf4j.Slf4j;
import lombok.val;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.*;

/**
 * @author : xiaogao
 * @date : 2024-11-14 14:00
 * @description :
 **/
@RestController
@RequestMapping("/user/user")
@Slf4j
@RefreshScope
public class TbUserController {

    @Autowired
    TbUserService tbUserService;
    @Autowired
    TbUserMapper tbUserMapper;

//    @Value("${yhq.gz}")
//    String nickName = "gz";

//    @PostMapping("/framework")
//    public R framework (){
//        return R.OK(nickName);
//    }


    @PostMapping(value = "/getUserById/{userId}")
    public R getUserById(@PathVariable("userId") Integer userId){
        TbUser tbUser = tbUserService.getUserById(userId);
        if(tbUser == null){
            return R.ERROR(404,"用户不存在");
        }
        return R.OK(tbUser);
    }

    @PostMapping(value = "/login")
    public R login(@RequestBody LoginInfoVo loginInfoVo){
        return tbUserService.login(loginInfoVo);
    }

    @PostMapping(value = "/listPage")
    public R listPage(@RequestBody PageInfoVo pageInfoVo){
        Page<TbUser> pages = new Page<>(pageInfoVo.getPageNum(), pageInfoVo.getPageSize());
        QueryWrapper<TbUser> wrapper = new QueryWrapper<>();
        if(!StringUtils.isEmpty(pageInfoVo.getKeyWord())){
            wrapper.lambda().like(TbUser::getUserName,pageInfoVo.getKeyWord());
        }
        Page<TbUser> page = tbUserMapper.selectPage(pages, wrapper);
        return R.OK(page);
    }

    @PostMapping(value = "/padd")
    public R padd(@RequestBody TbUser tbUser){
        LambdaQueryWrapper<TbUser> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(TbUser::getUserName,tbUser.getUserName());
        TbUser usrDb = tbUserMapper.selectOne(wrapper);
        if(usrDb!=null){
            return R.ERROR(400,"用户已存在");
        }
        tbUserMapper.insert(tbUser);
        return R.OK();
    }

    // 商家注册接口
    @PostMapping("/shopperRegister")
    public R shopperRegister(@RequestBody TbUser tbUser){
        return tbUserService.shopperRegister(tbUser);
    }

    /**
    * @Author: Malk
    * @Description: kafka发送消息
    * @Param:
    * @return:
    * @Date: 2024/11/19 20:27
    */

    @PostMapping("/sendKafkaMessage/{message}")
    public R sendKafkaMessage(@PathVariable("message") String message){
        System.out.println("发送消息"+message);
        return tbUserService.sendKafkaMessage(message);
//        return R.OK();
    }

}